package com.neusoft.gene.nep.grid.bufferedio;

import java.io.*;

/**
 * @Description: TODO  从文件中读取字符串和向文件中写入字符串。
 * @author: 君醉
 * @date: 2024/6/19 15:00
*/


public class BufferedFileIo {

    private  static final String filePath ="d:\\grid.json";

    //使用 BufferedReader 从指定文件路径读取内容，并将其存储在一个 StringBuilder 对象中，然后将其转换为字符串并返回。
    public static String readStringFromFile(   ) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
        }
        return content.toString();
    }

    //使用 BufferedWriter 将字符串写入指定文件路径。
    public  static void writeStringToFile( String content) throws IOException {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))){
            writer.write(content);
        }
    }


}
